<template>
  <div class="member-info">
    <div class="page-header">
      <h2>成员信息</h2>
      <p>项目成员管理</p>
    </div>
    
    <el-card class="member-card">
      <div class="toolbar">
        <span class="toolbar-label">切换项目</span>
        <el-select v-model="selectedProjectId" placeholder="请选择项目" class="project-select" @change="handleProjectChange">
          <el-option v-for="p in projects" :key="p.id" :label="p.projectName" :value="p.id" />
        </el-select>
      </div>

      <div class="leaders">
        <div class="leader-row">
          <span class="leader-label">前端负责人</span>
          <el-input v-model="frontendLeaderName" disabled class="leader-input" />
        </div>
        <div class="leader-row">
          <span class="leader-label">后端负责人</span>
          <el-input v-model="backendLeaderName" disabled class="leader-input" />
        </div>
      </div>

      <div class="module-section">
        <h3 class="section-title">模块管理区域</h3>
        <div class="module-list">
          <el-card v-for="m in displayedModules" :key="m.id" class="module-item" shadow="never">
            <div class="module-header">
              <div>
                <strong>{{ m.moduleName }}</strong>
                <span class="module-type">{{ m.moduleType === 1 ? '前端模块' : '后端模块' }}</span>
              </div>
              <div class="module-leader">负责人：{{ m.leaderName }}</div>
            </div>
            <div class="module-body">
              <div class="progress-row">
                <span class="progress-label">进度</span>
                <el-slider v-model="progressByModuleId[m.id]" :disabled="!canEditModule(m)" show-input :min="0" :max="100" class="progress-slider" />
                <el-button type="primary" :disabled="!canEditModule(m)" @click="saveProgress(m)">更新</el-button>
              </div>
              <div class="module-desc">{{ m.description }}</div>
            </div>
          </el-card>
        </div>
      </div>
    </el-card>
  </div>
  
</template>

<script setup>
import { ref, reactive, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { mockProjects, mockProjectModules } from '@/api/mockData'

const userStore = useUserStore()

// 项目数据与选择
const projects = ref([...mockProjects])
const selectedProjectId = ref(projects.value[0]?.id || null)

// 模块列表（按项目过滤）
const displayedModules = computed(() => mockProjectModules.filter(m => m.projectId === selectedProjectId.value))

// 负责人名称从模块中推断
const frontendLeaderName = computed(() => {
  const m = displayedModules.value.find(x => x.moduleType === 1)
  return m?.leaderName || '未指定'
})
const backendLeaderName = computed(() => {
  const m = displayedModules.value.find(x => x.moduleType === 2)
  return m?.leaderName || '未指定'
})

// 模块进度本地状态（不改动 mockData）
const progressByModuleId = reactive({})

const initProgress = () => {
  displayedModules.value.forEach(m => {
    if (progressByModuleId[m.id] === undefined) {
      progressByModuleId[m.id] = 0
    }
  })
}

onMounted(() => {
  initProgress()
})

const handleProjectChange = () => {
  initProgress()
}

// 权限：模块负责人或部长可编辑
const canEditModule = (module) => {
  const isLeader = userStore.userInfo?.id === module.leaderId
  return isLeader || userStore.isMinister
}

const saveProgress = (module) => {
  const value = progressByModuleId[module.id]
  ElMessage.success(`${module.moduleName} 进度已更新为 ${value}%`)
}
</script>

<style scoped>
.member-info {
  padding: 0;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  color: #303133;
  margin-bottom: 8px;
}

.page-header p {
  color: #606266;
  font-size: 14px;
}

.member-card {
  padding: 12px 12px 20px 12px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  margin-bottom: 12px;
}

.toolbar-label {
  color: #606266;
}

.project-select {
  width: 260px;
}

.leaders {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 12px;
}

.leader-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.leader-label {
  width: 100px;
  color: #303133;
}

.leader-input {
  max-width: 360px;
}

.module-section {
  margin-top: 8px;
}

.section-title {
  font-size: 16px;
  color: #303133;
  margin: 12px 0;
  font-weight: 500;
}

.module-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.module-item {
  border: 1px solid #e4e7ed;
  border-radius: 6px;
}

.module-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.module-type {
  margin-left: 10px;
  color: #909399;
  font-size: 12px;
}

.module-leader {
  color: #606266;
}

.module-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.progress-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.progress-label {
  width: 60px;
  color: #606266;
}

.progress-slider {
  flex: 1;
  max-width: 420px;
}

@media (max-width: 768px) {
  .leader-input {
    max-width: 100%;
  }
}
</style>